//
//  LLViewController.m
//  HuateVideo
//
//  Created by Tiger on 12-7-23.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LLViewController.h"
#import <QuartzCore/QuartzCore.h>
#include <ifaddrs.h>
#include <sys/socket.h>
#include <net/if.h>

@interface LLViewController ()

@end

@implementation LLViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    gprs = 0;
    wifi = 0;
    
    [self getDataCounters];
    
    array = [[NSMutableArray alloc]initWithObjects:LS(@"开机总流量(仅供参考):"),@"2G/3G",@"WIFI", nil];
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStyleGrouped];
    table.delegate = self;
    table.dataSource = self;
    table.backgroundColor = [UIColor clearColor];
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    table.separatorColor=LIGHT_COLOR;
    table.allowsSelection = NO;
    [self.view addSubview:table];
}

- (void)getDataCounters
{
    BOOL   success;
    struct ifaddrs *addrs;
    const struct ifaddrs *cursor;
    const struct if_data *networkStatisc; 
    
    int WiFiSent = 0;
    int WiFiReceived = 0;
    int WWANSent = 0;
    int WWANReceived = 0;
    
    NSString *name=[[[NSString alloc]init]autorelease];
    
    success = getifaddrs(&addrs) == 0;
    if (success) 
    {
        cursor = addrs;
        while (cursor != NULL) 
        {
            name=[NSString stringWithFormat:@"%s",cursor->ifa_name];
            //NSLog(@"ifa_name %s == %@\n", cursor->ifa_name,name);
            // names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN 
            
            if (cursor->ifa_addr->sa_family == AF_LINK) 
            {
                if ([name hasPrefix:@"en"]) 
                {
                    networkStatisc = (const struct if_data *) cursor->ifa_data;
                    WiFiSent+=networkStatisc->ifi_obytes;
                    WiFiReceived+=networkStatisc->ifi_ibytes;
                    //NSLog(@"WiFiSent %d ==%d",WiFiSent,networkStatisc->ifi_obytes);
                    //NSLog(@"WiFiReceived %d ==%d",WiFiReceived,networkStatisc->ifi_ibytes);
                }
                
                if ([name hasPrefix:@"pdp_ip"]) 
                {
                    networkStatisc = (const struct if_data *) cursor->ifa_data;
                    WWANSent+=networkStatisc->ifi_obytes;
                    WWANReceived+=networkStatisc->ifi_ibytes;
                    //NSLog(@"WWANSent %d ==%d",WWANSent,networkStatisc->ifi_obytes);
                    //NSLog(@"WWANReceived %d ==%d",WWANReceived,networkStatisc->ifi_ibytes);
                } 
            }
            
            cursor = cursor->ifa_next;
        }
        
        freeifaddrs(addrs);
    }
    
    gprs = WWANSent + WWANReceived;
    wifi = WiFiSent + WiFiReceived;
}

- (NSString *)getLiuliangString:(long long)liuliang
{
    double ll = liuliang;
    NSString *rlt;
    NSArray *arrayDW = [NSArray arrayWithObjects:@"B",@"KB",@"MB",@"GB",@"TB", nil];
    NSInteger i=0;
    while (ll>=1024) {
        i++;
        ll = (double)ll/1024.0;
    }
    
    if (i<5) {
        rlt = [NSString stringWithFormat:@"%.1f%@",ll,[arrayDW objectAtIndex:i]];
    }
    else {
        rlt = @"未知";
    }
    return rlt;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* ListIdentifier = @"ListIdentifier1";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ListIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ListIdentifier];
        //cell.selectionStyle = UITableViewCellSelectionStyleNone;  //去掉选中时的颜色
    }
    
    for (UIView* subView in [cell.contentView subviews])
    {
        [subView removeFromSuperview];
    }
    
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    if (indexPath.row == 1) {
        UILabel *valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 200, 25)];
        valueLabel.textAlignment = UITextAlignmentRight;
        valueLabel.textColor = LIGHT_COLOR;
        valueLabel.text = [self getLiuliangString:gprs];
        valueLabel.backgroundColor = [UIColor clearColor];
        valueLabel.font = [UIFont boldSystemFontOfSize:16.0];
        [cell.contentView addSubview:valueLabel];
        [valueLabel release];
    }
    else if (indexPath.row == 2){
        UILabel *valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 200, 25)];
        valueLabel.textAlignment = UITextAlignmentRight;
        valueLabel.textColor = LIGHT_COLOR;
        valueLabel.text = [self getLiuliangString:wifi];
        valueLabel.backgroundColor = [UIColor clearColor];
        valueLabel.font = [UIFont boldSystemFontOfSize:16.0];
        [cell.contentView addSubview:valueLabel];
        [valueLabel release];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

@end
